public class RadixConvertUtil {

    // 0-9a-zA-Z char for radix 2~62
    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z'
        };

    // converts a decimal number to a specified raidx string
    public static String toRadix(long num, int radix) {
        // complement code for negative
        if (num < 0) { num = ((long) 0xffffffff) + num + 1; }

        char[] buf = new char[32];  // long 4*8=32
        int idx = 32;

        while ((num / radix) > 0) {
            buf[--idx] = DIGITS[(int) (num % radix)];
            num /= radix;
        }
        buf[--idx] = DIGITS[(int) (num % radix)];
        return new String(buf, idx, (32 - idx));
    }


    // convert radix digits (in string form) to decimal digit
    public static long toDecimal(String num, int radix) {
        if (radix == 10) { return Long.parseLong(num); }

        char[] buf = num.toCharArray();

        long result = 0, base = 1;

        for (int i = buf.length - 1; i >= 0; i--) {
            int index = 0;
            for (int j = 0, length = DIGITS.length; j < length; j++) {
                if (DIGITS[j] == buf[i]) { index = j; }
            }
            result += index * base;
            base *= radix;
        }
        return result;
    }


    public static void main(String[] args) {
        if (args.length == 1) {
            System.out.println(RadixConvertUtil.toRadix(Long.parseLong(args[0]), 62));
        }
        else if (args.length == 2) {
            System.out.println(RadixConvertUtil.toRadix(Long.parseLong(args[0]), Integer.parseInt(args[1])));
        }
        else {
            System.out.println(RadixConvertUtil.toDecimal(args[0], Integer.parseInt(args[1])));
        }
    }
}
